﻿//==================================================================
// Copyright (C) 2015 福流网络
// 作 者：江怀玉
// 日 期：2015-04-22
// 描 述： 用于序列化和反序列化的工具类
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
//==================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;

namespace FL.Common.Extend
{

    public sealed class JsonSerializer<T>
    {
        /// <summary>
        /// 将类的实例序列化为xml字符串
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static string SerializeToXml(T instance)
        {
            //序列化            
            string xmlString = String.Empty;

            XmlSerializer xmlSerializer = new XmlSerializer(instance.GetType());
            MemoryStream stream = new MemoryStream();
            xmlSerializer.Serialize(stream, instance);
            byte[] buf = stream.ToArray();
            xmlString = Encoding.UTF8.GetString(buf);
            stream.Close();

            return xmlString;
        }
        /// <summary>
        /// 将xml字符串反序列化为类的实例
        /// </summary>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public static T DeserializeFromXml(string xmlString)
        {
            //反序列化
            T obj = default(T);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            byte[] buf = Encoding.ASCII.GetBytes(xmlString);
            MemoryStream stream = new MemoryStream(buf);
            obj = (T)xmlSerializer.Deserialize(stream);
            stream.Close();

            return obj;
        }

        /// <summary>
        /// 转换DataContract数据对象为json串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="value">值</param>
        /// <returns>返回DataContract数据对象的json串</returns>
        public static string DataContractToJson(T v)
        {
            MemoryStream stream1 = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            ser.WriteObject(stream1, v);
            stream1.Position = 0;
            using (StreamReader sr = new StreamReader(stream1))
            {
                return sr.ReadToEnd();
            }
        }

        /// <summary>
        /// 转换DataContract数据对象为json串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="value">值</param>
        /// <returns>返回DataContract数据对象的json串</returns>
        public static T JsonToDataContract(string value)
        {
            //反序列化
            T obj = default(T);

            DataContractJsonSerializer xmlSerializer = new DataContractJsonSerializer(typeof(T));
            byte[] buf = Encoding.Unicode.GetBytes(value);
            MemoryStream stream = new MemoryStream(buf);
            obj = (T)xmlSerializer.ReadObject(stream);
            stream.Close();

            return obj;
        }

    }
}
